package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.pojo.RecommendUser;
import com.tanhua.dubbo.pojo.UserInfo;
import com.tanhua.dubbo.vo.PageInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TodayBestService {
    @Autowired
    private UserService userService;
    @DubboReference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;
    //TODO 可以进一步的优化，将默认配置写入到nacos中
    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;

    @Value("${tanhua.sso.default.users}")
    private Long[] defaultUsers;

    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;


    public TodayBest queryTodayBest(String token) {
        //检查token是否有效
        Long userId = this.userService.checkToken(token);
        if (null == userId) {
            return null;
        }

        //通过远程服务调用今日佳人
        RecommendUser recommendUser = this.recommendUserApi.queryWithMaxScore(userId);
        //recommendUser为null;
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(defaultUser);
            recommendUser.setToUserId(userId);
            recommendUser.setScore(RandomUtil.randomDouble(80, 99));

        }
        //recommendUser为不null;
        TodayBest todayBest = new TodayBest();
        todayBest.setFateValue(recommendUser.getScore().longValue());   //设置缘分值
        //查询用户信息
        UserInfo userInfo = this.userInfoApi.queryByUserId(recommendUser.getUserId());
        BeanUtil.copyProperties(userInfo, todayBest, "id");
        todayBest.setId(userInfo.getUserId());
        // 性别，将枚举的name转成小写
        todayBest.setGender(userInfo.getSex().name().toLowerCase());

        return todayBest;
    }

    public PageResult qurryRecommendation(String token, RecommendUserQueryParam queryParam) {
        //检查token是否有效
        Long userId = this.userService.checkToken(token);
        if (null == userId) {
            return null;
        }

        PageResult pageResult = new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());
        //通过远程服务调用推荐列表
        PageInfo<RecommendUser> pageInfo = this.recommendUserApi.queryPageInfo(userId, queryParam.getPage(), queryParam.getPagesize());
        List<RecommendUser> recommendUserList = pageInfo.getRecords();
        if (CollUtil.isEmpty(recommendUserList)) {
            //todo  给出默认推荐的
            recommendUserList = new ArrayList<>();
            for (Long uid : defaultUsers) {
                RecommendUser recommendUser = new RecommendUser();
                recommendUser.setToUserId(userId);
                recommendUser.setUserId(uid);
                recommendUser.setScore(RandomUtil.randomDouble(80, 99));
                recommendUserList.add(recommendUser);
            }
        }
        //recommendUserList != null;
        ArrayList<TodayBest> todayBestList = new ArrayList<>();
        //获取到推荐的用户id列表
        List<Object> userIdList = CollUtil.getFieldValues(recommendUserList, "userId");
        //查询用户信息
        List<UserInfo> userInfoList = this.userInfoApi.queryByUserIdList(userIdList);

        for (RecommendUser recommendUser : recommendUserList) {
            TodayBest todayBest = new TodayBest();
            todayBest.setFateValue(recommendUser.getScore().longValue());
            todayBest.setId(recommendUser.getUserId());

            for (UserInfo userInfo : userInfoList) {
                if (ObjectUtil.equal(userInfo.getUserId(), recommendUser.getUserId())) {
                    BeanUtil.copyProperties(userInfo, todayBest, "id");
                    // 性别，将枚举的name转成小写
                    todayBest.setGender(userInfo.getSex().name().toLowerCase());
                    break;
                }
            }
            todayBestList.add(todayBest);
        }
        pageResult.setItems(todayBestList);
        return pageResult;
    }
}